package com.ruoyi.job.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.job.domain.JobHelp;
import com.ruoyi.job.domain.JobStudent;
import com.ruoyi.job.domain.JobTeacher;
import com.ruoyi.job.mapper.JobHelpMapper;
import com.ruoyi.job.mapper.JobStudentMapper;
import com.ruoyi.job.mapper.JobTeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.job.mapper.JobHelpTransferMapper;
import com.ruoyi.job.domain.JobHelpTransfer;
import com.ruoyi.job.service.IJobHelpTransferService;

/**
 * 帮扶移交Service业务层处理
 * 
 * @author LYD
 * @date 2023-02-01
 */
@Service
public class JobHelpTransferServiceImpl implements IJobHelpTransferService 
{
    @Autowired
    private JobHelpTransferMapper jobHelpTransferMapper;
    @Autowired
    private JobTeacherMapper teacherMapper;
    @Autowired
    private JobStudentMapper studentMapper;
    @Autowired
    private JobHelpMapper helpMapper;

    /**
     * 查询帮扶移交
     * 
     * @param id 帮扶移交主键
     * @return 帮扶移交
     */
    @Override
    public JobHelpTransfer selectJobHelpTransferById(Long id)
    {
        return jobHelpTransferMapper.selectJobHelpTransferById(id);
    }

    /**
     * 查询帮扶移交列表
     * 
     * @param jobHelpTransfer 帮扶移交
     * @return 帮扶移交
     */
    @Override
    public List<JobHelpTransfer> selectJobHelpTransferList(JobHelpTransfer jobHelpTransfer)
    {
        return jobHelpTransferMapper.selectJobHelpTransferList(jobHelpTransfer);
    }

    /**
     * 新增帮扶移交
     * 
     * @param jobHelpTransfer 帮扶移交
     * @return 结果
     */
    @Override
    public int insertJobHelpTransfer(JobHelpTransfer jobHelpTransfer)
    {
        //请求来了，先根据帮扶id去找移交表里有多少条数据
        List<JobHelpTransfer> transfers = jobHelpTransferMapper.findByHelpId(jobHelpTransfer.getHelpid());
        //如果条数<3,放行,否则抛异常
        if(transfers.size()>=3){
            throw new BaseException("移交失败,最多移交3次！");
        }else {

        //1.添加移交对象
        //添加移交时间
        jobHelpTransfer.setTransfertime(new Date());
        //获取接收人的id
        Long receiverid = jobHelpTransfer.getReceiverid();
        //获取接收人对象
        JobTeacher receiverTeacher = teacherMapper.selectJobTeacherById(receiverid);
        //获取接收人名字
        String teachername = receiverTeacher.getTeachername();
        //设置接受人名字
        jobHelpTransfer.setReceivername(teachername);

        //2.修改学生管理
        //修改帮扶老师id
        //修改状态 移交他人4
        JobStudent student = studentMapper.selectJobStudentById(jobHelpTransfer.getHelpid());
        student.setTecherId(receiverid);
        student.setState(4);
        studentMapper.updateJobStudent(student);


        //3.修改帮扶管理
        //获取当前帮扶信息
        JobHelp jobHelp = helpMapper.selectJobHelpById(jobHelpTransfer.getHelpid());
        jobHelp.setState(4);// 移交他人4
        jobHelp.setTecherId(receiverid);//修改帮扶老师id
        jobHelp.setTeachernote("已移交给帮扶老师:"+teachername);//设置备注
        helpMapper.updateJobHelp(jobHelp);


        //保存移交对象
        return jobHelpTransferMapper.insertJobHelpTransfer(jobHelpTransfer);

        }
    }

    /**
     * 修改帮扶移交
     * 
     * @param jobHelpTransfer 帮扶移交
     * @return 结果
     */
    @Override
    public int updateJobHelpTransfer(JobHelpTransfer jobHelpTransfer)
    {
        return jobHelpTransferMapper.updateJobHelpTransfer(jobHelpTransfer);
    }

    /**
     * 批量删除帮扶移交
     * 
     * @param ids 需要删除的帮扶移交主键
     * @return 结果
     */
    @Override
    public int deleteJobHelpTransferByIds(Long[] ids)
    {
        return jobHelpTransferMapper.deleteJobHelpTransferByIds(ids);
    }

    /**
     * 删除帮扶移交信息
     * 
     * @param id 帮扶移交主键
     * @return 结果
     */
    @Override
    public int deleteJobHelpTransferById(Long id)
    {
        return jobHelpTransferMapper.deleteJobHelpTransferById(id);
    }
}
